<template>
  <div>
    <div class="header">
      <el-button type="default" icon="el-icon-s-unfold" style="margin-left: 10px" size="medium"
        @click="functionVis = true">
        功能
      </el-button>
    </div>
    <div class="main">
      <el-table style="overflow: auto" :data="res" stripe border highlight-current-row show-summary>
        <el-table-column type="index" width="60"></el-table-column>
        <el-table-column label="案件">
          <template slot-scope="scope">
            <el-link v-if="scope.row.CS_No" type="primary" @click="toCase(scope.row.CS_ID)">
              流水号：{{ scope.row.CS_No }}
            </el-link>
            <div v-if="scope.row.CS_DisplayName">
              案件名：{{ scope.row.CS_DisplayName }}
            </div>
          </template>
        </el-table-column>
        <el-table-column label="开票金额">
          <template slot-scope="scope">
            共{{ scope.row.ChargeSum }}元
            <div v-for="item in scope.row.Charges" :key="item.CSCG_ID" style="color: #888; font-size: 12px">
              【{{ item.CHTY_EnumTxt }}】{{ item.CSCG_Fee }}元
            </div>
          </template>
        </el-table-column>
        <el-table-column label="平台回款">
          <template slot-scope="scope">
            共{{ scope.row.ReturnSum }}元
            <div v-for="item in scope.row.Returns" :key="item.CSBR_ID" style="color: #888; font-size: 12px">
              【{{ item.CSBR_AccountTime | formatDate }}】{{ item.CSBR_Fee }}元
            </div>
          </template>
        </el-table-column>
        <el-table-column label="案件成本">
          <template slot-scope="scope">
            共{{ scope.row.CostSum }}元
            <div v-for="item in scope.row.Costs" :key="item.CSC_ID" style="color: #888; font-size: 12px">
              【{{ item.CSC_Name ? item.CSC_Name : '未知类型' }}】{{
                item.CSC_Fee
              }}元
            </div>
          </template>
        </el-table-column>

        <el-table-column label="提成合计" prop="EmpIncomeSum">
          <template slot-scope="scope">{{ scope.row.EmpIncomeSum }}元</template>
        </el-table-column>
        <el-table-column label="提成明细" type="expand" width="120">
          <template slot-scope="scope">
            <div v-for="item in scope.row.EmpIncomes" :key="item.CEI_ID" style="margin: 5px">
              <span style="margin-left: 20px" v-if="item.CSBR_Fee">
                【 {{ item.CSBR_AccountTime | formatDate }}日回款:{{
                  item.CSBR_Fee
                }}元】
              </span>
              <span style="margin-left: 20px">
                【{{ item.CEI_Name }}】{{ item.USR_Name }}
              </span>
              <span style="margin-left: 20px">
                应付金额:{{ item.CEI_Fee }}元
              </span>
              <span style="margin-left: 20px">回款率:{{ item.CEI_Rate }}%</span>
              <span style="margin-left: 20px" v-if="item.CEI_Remark">
                备注:{{ item.CEI_Remark }}
              </span>
            </div>
          </template>
        </el-table-column>
      </el-table>
    </div>

    <el-dialog title="功能操作" :visible.sync="functionVis" width="500px" :modal-append-to-body="false" center>
      <el-form label-width="100px">
        <el-form-item label="关键字">
          <el-input placeholder="请输入关键字" v-model="req.keyword" style="width: 240px"></el-input>
        </el-form-item>
        <el-form-item label="平台回款日期">
          <el-date-picker type="date" v-model="req.accountDate1" placeholder="请选择日期" value-format="yyyy-MM-dd HH:mm:ss"
            style="width: 140px"></el-date-picker>
          至
          <el-date-picker type="date" v-model="req.accountDate2" placeholder="请选择日期" value-format="yyyy-MM-dd HH:mm:ss"
            style="width: 140px"></el-date-picker>
        </el-form-item>
        <el-form-item label="机构回款日期">
          <el-date-picker type="date" v-model="req.cmpAccountDate1" placeholder="请选择日期"
            value-format="yyyy-MM-dd HH:mm:ss" style="width: 140px"></el-date-picker>
          至
          <el-date-picker type="date" v-model="req.cmpAccountDate2" placeholder="请选择日期"
            value-format="yyyy-MM-dd HH:mm:ss" style="width: 140px"></el-date-picker>
        </el-form-item>

        <el-form-item label="回款登记日期">
          <el-date-picker type="date" v-model="req.returnCreateDate1" placeholder="请选择日期"
            value-format="yyyy-MM-dd HH:mm:ss" style="width: 140px"></el-date-picker>
          至
          <el-date-picker type="date" v-model="req.returnCreateDate2" placeholder="请选择日期"
            value-format="yyyy-MM-dd HH:mm:ss" style="width: 140px"></el-date-picker>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" style="margin-left: 10px" size="small" @click="getOutworkerIncomes">
            查询提成
          </el-button>
          <el-button type="warning" style="margin-left: 10px" size="small" @click="createOutworkerIncomes">
            提成生成
          </el-button>
          <el-button type="success" style="margin-left: 10px" size="small" @click="outputExcel">
            导出Excel
          </el-button>
          <el-button type="text" style="margin-left: 10px" size="small" @click="functionVis = false">
            取消
          </el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>
<script>
import companyEmployeesChoose from '@/views/web/orgnization/components/companyEmployeesChoose.vue'
import {
  getOutworkerIncomes,
  createOutworkerIncomes,
} from '@/api/finance/caseEmployeeIncomeStas'
import { baseURL, tokenName, empIdName } from '@/config'
import axios from 'axios'

export default {
  components: { companyEmployeesChoose },
  data: function () {
    return {
      req: {
        keyword: null,
        accountDate1: null,
        accountDate2: null,
        cmpAccountDate1: null,
        cmpAccountDate2: null,
        returnCreateDate1: null,
        returnCreateDate2: null,
      },
      res: [],
      functionVis: false,
    }
  },
  methods: {
    getOutworkerIncomes() {
      getOutworkerIncomes(this.req).then(({ data }) => {
        this.res = data
        this.functionVis = false
      })
    },
    toCase: function (caseId) {
      this.$router.push({
        name: 'caseInfoPage',
        query: { caseId },
      })
    },
    createOutworkerIncomes() {
      this.$confirm(`确认生成提成？`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      }).then(() => {
        createOutworkerIncomes(this.req).then(() => {
          this.functionVis = false
          this.$message('生成成功')
          this.getOutworkerIncomes()
        })
      })
    },

    outputExcel() {
      var config = {
        baseURL,
        headers: {},
        responseType: 'blob',
      }
      config.headers[tokenName] = this.$store.getters['user/accessToken']
      config.headers[empIdName] = this.$store.getters['user/curEmp'].EMP_ID
      axios
        .post(
          '/api/Finance/CaseEmployeeIncomeStas/OutputOutworkerExcel',
          this.req,
          config
        )
        .then((response) => {
          this.functionVis = false
          var blob = new Blob([response.data])
          var downloadElement = document.createElement('a')
          var href = window.URL.createObjectURL(blob) //创建下载的链接
          downloadElement.href = href
          downloadElement.download = '提成报表.xls' //下载后文件名
          document.body.appendChild(downloadElement)
          downloadElement.click() //点击下载
          document.body.removeChild(downloadElement) //下载完成移除元素
          window.URL.revokeObjectURL(href) //释放掉blob对象
        })
        .catch((error) => {
          console.log('response: ', error)
        })
    },
  },
  created: function () {
    var data = this.$store.getters['pagination/getRoutePage'](this.$route)
    Object.assign(this.$data, data)
  },
  beforeRouteLeave(to, from, next) {
    this.$store.dispatch('pagination/setRoutePage', {
      route: this.$route,
      pageData: this.$data,
    })
    next()
  },
}
</script>
<style scoped>
.header {
  width: 100%;
  padding-top: 10px;
  padding-bottom: 10px;
  height: auto;
  font-size: 14px;
  font-family: PingFangSC-Regular, PingFang SC;
  font-weight: 400;
  background: #ffffff;
  border-radius: 5px;
  border-bottom: #f4f8ff 2px solid;
  display: flex;
  margin-bottom: 11px;
  align-items: center;
}

.main {
  height: 75vh;
  overflow: auto;
}
</style>
